package com.tust.app.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.dao.AnswerMapper;
import com.tust.app.domain.Answer;
import com.tust.app.domain.Topic;
import com.tust.app.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerRepository;

    @Override
    public PageInfo getAllAnswers(int pageNum) {

        PageHelper.startPage(pageNum, 3);
        //查询数据库,得到新闻表中的数据列表,丢给页面
        List<Answer> answers = answerRepository.findAllAnswers();
        PageInfo page = new PageInfo(answers);
        return page;
    }
    @Override
    public void updateAnswerState(int id) {
        Answer answer = answerRepository.findById(id);
        if (answer != null) {

            int state2=answer.getState2()==0?1:0;
            answerRepository.update(state2,id);
        }
    }

    @Override
    public void deleteAnswer(int id) {
        answerRepository.deleteById(id);
    }

    @Override
    public void addAnswer(Answer answer) {
        answerRepository.save(answer);
    }


}
